package weektwo;

import java.util.HashSet;
import java.util.Set;

public class HappyNumber_202 {
    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (true) {
            n = getNext(n);
            if (n != 1 && !set.contains(n)) {
                set.add(n);
            } else {
                return n == 1;
            }
//            if (n == 1) return true;
//            else if (!set.contains(n)) {
//                set.add(n);
//            } else return false;
        }

    }

    private int getNext(int n) {
        int next = 0;
        while (n != 0) {
            int d = n % 10;
            next += d * d;
            n = n / 10;
        }
        return next;
    }
}
